﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;

namespace Waiter.Controler
{
    class UserCtrl
    {
        /// <summary>
        /// get user info by name
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public USER fsGetUserByName(string userName)
        {
            USER user = null;
            try
            {
                user = CommonGlobalVariable.globalDataContext.USERs.First(q => q.USER_NAME == userName);
            }
            catch(Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsGetUserByName",
                           CommonUtils.fsParamListToString(new object[1] { userName }),
                           mex.ToString());
            }
            return user;
        }

        /// <summary>
        /// get user by code
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public USER fsGetUserByCode(string code)
        {
            USER user = null;
            try
            {
                user = CommonGlobalVariable.globalDataContext.USERs.First(q => q.CODE == code);
            }
            catch (Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsGetUserByCode",
                           CommonUtils.fsParamListToString(new object[1] { code }),
                           mex.ToString());
            }
            return user;
        }

        /// <summary>
        /// check username is exist in USER table
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool fsIsExistUser(string userName)
        {
            if (fsGetUserByName(userName) != null)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// get all user list
        /// </summary>
        /// <returns></returns>
        public List<USER> fsGetAllUser()
        {
            List<USER> resultList = null;
            try
            {
                resultList = (from s in CommonGlobalVariable.globalDataContext.USERs
                                         select s).ToList<USER>();
            }
            catch(Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsGetAllUser",
                           "",
                           mex.ToString());
            }
            return resultList;
        }

        /// <summary>
        /// delete user by userCode (set delFlg = false)
        /// </summary>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public ResultStatus fsDeleteUser(string userCode)
        {
            ResultStatus result = ResultStatus.ERROR;
            try
            {
                USER item = CommonGlobalVariable.globalDataContext.USERs.First(q=>q.CODE == userCode);
                item.DEL_FLAG = CommonConstants.FLAG_ON;
                CommonGlobalVariable.globalDataContext.SubmitChanges();
                result = ResultStatus.SUCCESS;
            }
            catch (Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsDeleteUser",
                           CommonUtils.fsParamListToString(new object[1] { userCode }),
                           mex.ToString());
            }
            return result;
        }

        /// <summary>
        /// Add new user
        /// </summary>
        /// <param name="staffCode"></param>
        /// <param name="typeCode"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ResultStatus fsAddNewUser(string code, string staffCode, int typeCode, string userName, string password)
        {
            ResultStatus result = ResultStatus.ERROR;
            try
            {
                USER item = new USER();
                item.CODE = code;
                item.USER_NAME = userName;
                item.PASS_WORD = password;
                item.CODE_TYPE = typeCode;
                item.CODE_STAFF = staffCode;
                item.DEL_FLAG = CommonConstants.FLAG_OFF;
                item.OPID = CommonUtils.fsGetOPID();
                DateTime currentDate = DateTime.Now;
                item.UPD_DATE = currentDate;
                item.ADD_DATE = currentDate;

                CommonGlobalVariable.globalDataContext.USERs.InsertOnSubmit(item);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
                result = ResultStatus.SUCCESS;
            }
            catch (Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsAddNewUser",
                           CommonUtils.fsParamListToString(new object[5] { code, staffCode, typeCode, userName, password }),
                           mex.ToString());
            }
            return result;
        }

        /// <summary>
        /// Update user info
        /// </summary>
        /// <param name="staffCode"></param>
        /// <param name="typeCode"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ResultStatus fsUpdateUser(string userCode, int typeCode, string password)
        {
            ResultStatus result = ResultStatus.ERROR;
            try
            {
                USER item = CommonGlobalVariable.globalDataContext.USERs.First(q=>q.CODE == userCode);
                item.PASS_WORD = password;
                item.CODE_TYPE = typeCode;
                item.OPID = CommonUtils.fsGetOPID();
                item.UPD_DATE = DateTime.Now;

                CommonGlobalVariable.globalDataContext.USERs.InsertOnSubmit(item);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
                result = ResultStatus.SUCCESS;
            }
            catch (Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsUpdateUser",
                           CommonUtils.fsParamListToString(new object[4] { userCode, typeCode, typeCode, password}),
                           mex.ToString());
            }
            return result;
        }

        /// <summary>
        /// count all user on table USER
        /// </summary>
        /// <param name="countDeleteUser"></param>
        /// <returns></returns>
        public int fsCountAllUser(bool countDeleteUser)
        {
            int count = 0;
            try
            {
                if (countDeleteUser)
                {
                    count = CommonGlobalVariable.globalDataContext.USERs.Count();
                }
                else
                {
                    count = CommonGlobalVariable.globalDataContext.USERs.Count(q => q.DEL_FLAG == CommonConstants.FLAG_ON);
                }
            }
            catch(Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsCountAllUser",
                           CommonUtils.fsParamListToString(new object[1] { countDeleteUser }),
                           mex.ToString());
            }
            return count;
        }

        public USER fsGetUserByStaff(string staffCode)
        {
            try
            {
                USER item = CommonGlobalVariable.globalDataContext.USERs
                    .FirstOrDefault(q => q.DEL_FLAG == CommonConstants.FLAG_OFF && q.CODE_STAFF == staffCode);
                return item;
            }
            catch (Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsGetUserByStaff",
                           CommonUtils.fsParamListToString(new object[1] { staffCode }),
                           mex.ToString());
                return null;
            }
        }

        /// <summary>
        /// insert or update
        /// </summary>
        /// <param name="staffCode"></param>
        /// <param name="typeCode"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ResultStatus fsInsertOrUpdateUser(string staffCode, int typeCode, string userName, string password)
        {
            try
            {
                DateTime currentDate = DateTime.Now;
                USER item = this.fsGetUserByStaff(staffCode);
                if (item == null)
                {
                    item = new USER();
                    item.CODE = CommonUtils.fsCreateNewCode();
                    item.USER_NAME = userName;
                    item.PASS_WORD = password;
                    item.CODE_TYPE = typeCode;
                    item.CODE_STAFF = staffCode;
                    item.DEL_FLAG = CommonConstants.FLAG_OFF;
                    item.OPID = CommonUtils.fsGetOPID();
                    item.UPD_DATE = currentDate;
                    item.ADD_DATE = currentDate;
                    CommonGlobalVariable.globalDataContext.USERs.InsertOnSubmit(item);
                }
                else
                {
                    item.USER_NAME = userName;
                    item.PASS_WORD = password;
                    item.CODE_TYPE = typeCode;
                    item.OPID = CommonUtils.fsGetOPID();
                    item.UPD_DATE = currentDate;
                }

                CommonGlobalVariable.globalDataContext.SubmitChanges();
                return ResultStatus.SUCCESS;
            }
            catch (Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsInsertOrUpdateUser",
                           CommonUtils.fsParamListToString(new object[4] { staffCode, typeCode, userName, password }),
                           mex.ToString());
                return ResultStatus.ERROR;
            }
        }

        public int fsGetUserType(string staffCode)
        {
            try
            {
                USER item = fsGetUserByStaff(staffCode);
                if (item != null)
                {
                    return item.CODE_TYPE;
                }
                else
                {
                    return (int)UserType.USER;
                }
            }
            catch (Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsGetUserType",
                           CommonUtils.fsParamListToString(new object[1] { staffCode }),
                           mex.ToString());
                return (int) UserType.USER;
            }
        }

        public ResultStatus fsDeleteUserByStaffCode(string staffCode)
        {
            try
            {
                USER item = this.fsGetUserByStaff(staffCode);
                if (item == null)
                {
                    return ResultStatus.SUCCESS;
                }
                item.DEL_FLAG = CommonConstants.FLAG_ON;
                item.UPD_DATE = DateTime.Now;
                item.OPID = CommonUtils.fsGetOPID();
                CommonGlobalVariable.globalDataContext.SubmitChanges();
                return ResultStatus.SUCCESS;
            }
            catch (Exception mex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "UserCtrl.fsDeleteUserByStaffCode",
                           CommonUtils.fsParamListToString(new object[1] { staffCode }),
                           mex.ToString());
                return ResultStatus.ERROR;
            }
        }
    }
}
